package com.iemes.controller.order;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.OrderFormMap;
import com.iemes.entity.StockbillinfoFormMap;
import com.iemes.mapper.OrderMapper;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/order/")
public class OrderController extends BaseController {
    @Inject
    private OrderMapper orderMapper;

    @RequestMapping("openUI")
    public String openUI(Model model){
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        //,"<button type=\"button\" id=\"view\" class=\"btn btn-primary marR10\">查看物料需求</button>"
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/order/orderManagement";
    }

    //获取分页数据
    @ResponseBody
    @RequestMapping("getOrderByPage")
    public PageInfo clientByPage(String pageNow,
                                 String pageSize, String column, String sort, HttpServletRequest request) throws Exception {
        String myValue = getPara("myValue");
        if(myValue == null || myValue.equals("")){
            if(pageNow == null){
                PageHelper.startPage(1, 10);
            }else {
                PageHelper.startPage(Integer.parseInt(pageNow), 10);
            }
            PageInfo pageInfo = new PageInfo<>(orderMapper.findOrderList());
            return pageInfo;

        }else {
            String[] split = myValue.split(",");
            if(split.length > 1){
                if(split[0].equals("name")){
                    //根据名称进行模糊查询
                    PageInfo pageInfo = new PageInfo<>(orderMapper.selectOrderData(split[1],"name"));
                    return pageInfo;
                }else {
                    //根据编号进行模糊查询
                    PageInfo pageInfo = new PageInfo<>(orderMapper.selectOrderData(split[1],"code"));
                    return pageInfo;
                }
            }else {
                List<OrderFormMap> orderFormMapList = orderMapper.findOrderList();
                PageInfo pageInfo = new PageInfo<>(orderFormMapList);
                return pageInfo;
            }
        }


//        if(pageNow == null){
//            PageHelper.startPage(1, 10);
//        }else {
//            PageHelper.startPage(Integer.parseInt(pageNow), 10);
//        }

        //产品名称
//        String product = request.getParameter("item_bom_no");
//        if(product == null || product.equals(" ") || product.equals("null") || product.equals("")){
//            List<OrderFormMap> orderFormMapList = orderMapper.findOrderList();
//            PageInfo pageInfo = new PageInfo<>(orderFormMapList);
//            return pageInfo;
//        }else {
//            String orderFormMapList = orderMapper.findStockName(product);
//            if(orderFormMapList != null || orderFormMapList.equals(" ")){
//                List<OrderFormMap> orderListId = orderMapper.findOrderListId(orderFormMapList);
//                PageInfo pageInfo = new PageInfo<>(orderListId);
//                return pageInfo;
//            }
//            return null;
//        }

    }

    //打开新增客户页面
    @RequestMapping("openOrderManagementUI")
    public String openOrderManagementUI(){
        return Common.BACKGROUND_PATH + "/order/addOrder";
    }

    //添加数据
    @ResponseBody
    @RequestMapping("addOrder")
    //    @SystemLog(module="系统管理",methods="客户管理-新增客户")//凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    public String addOrder(){
        OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);
        try {
            //根据成品id获取成品编码
//            String lyBom = orderMapper.findLyBom(orderFormMap.get("stockid").toString());
//            orderFormMap.put("stockid", lyBom);
            orderMapper.addEntity(orderFormMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }

    @RequestMapping("editOrderUI")
    public String editOrderUI(Model model, HttpServletRequest request){
        String id = getPara("id");
        if(Common.isNotEmpty(id)){
            //根据
            model.addAttribute("user", orderMapper.selectOrder(id));
        }
        return Common.BACKGROUND_PATH + "/order/editOrder";
    }

    @ResponseBody
    @RequestMapping("editOrder")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
//    @SystemLog(module="系统管理",methods="用户管理-修改用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editOrder(String txtGroupsSelect) throws Exception {
        OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);
        orderFormMap.put("txtGroupsSelect", txtGroupsSelect);
        orderMapper.deleteByAttribute("ofid", orderFormMap.get("ofid")+"", OrderFormMap.class);
//        String lyBom = orderMapper.findLyBom(orderFormMap.get("stockid").toString());
//        orderFormMap.put("stockid", lyBom);
        orderMapper.addEntity(orderFormMap);
        return "success";
    }

    @ResponseBody
    @RequestMapping("deleteOrder")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="系统管理",methods="用户管理-删除用户")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delOrder() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            orderMapper.deleteByAttribute("ofid", id, OrderFormMap.class);
        }
        return "success";
    }
    @RequestMapping("openMaterial")
    public String openMaterial(HttpServletRequest request, Model model){
        //根据订单号查询订单信息
        String id = request.getParameter("id");
        OrderFormMap orderFormMap = orderMapper.selectOrder(id);
        //找到成品id

        //根据成品id进行查找
        model.addAttribute("child",orderMapper.findChild(orderMapper.findLyBomId(orderFormMap.get("stockid").toString())));
        model.addAttribute("order", orderFormMap);
        return Common.BACKGROUND_PATH + "/order/materialInfo";
    }

}
